package panda.query.condition;

import panda.query.scan.Scanner;
import panda.query.struct.Attribute;
import panda.query.struct.Constant;
import panda.query.struct.SemAttribute;
import panda.record.Schema;
import panda.transaction.Transaction;

/**
 * The expression containing only an attribute name
 * This expression is atomic
 * @author Tian Yuan
 */
public class AttributeName implements ValueExpression {
	
	Attribute attr;
	Constant stub = null;

	/**
	 * 
	 * @param attr
	 */
	public AttributeName(Attribute attr) {
		this.attr = attr;
	}
	
	/**
	 * 
	 * @param s
	 */
	public Constant evaluate(Scanner s, Transaction tx) {
		if (stub != null) return stub;
		return s.getValue(attr);
	}

	public boolean dominates(Attribute attr) {
		if (attr == this.attr) return true;
		return false;
	}
	
	public Attribute getAttribute() {
		return attr;
	}

	public boolean isConstant() {
		return false;
	}

	public Constant asConstant() {
		return null;
	}

	public boolean appliesTo(Schema sch) {
		return sch.hasAttribute(attr);
	}
	
	/**
	 * Fill attribute with the attribute corresponding to certain table token
	 * @param attr
	 */
	public void instantiate(SemAttribute attr) {
		this.attr = attr;
	}
	
	public void stubValue(Constant c) {
		stub = c;
	}

}
